package com.example.app2;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


public class DataBaseActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv;
    private String mDatabase;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_datebase);
        findViewById(R.id.btn_create).setOnClickListener(this);
        findViewById(R.id.btn_delete).setOnClickListener(this);
        tv = findViewById(R.id.tv);

        mDatabase = getFilesDir() + "/test.db";
    }
    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.btn_create:
                //创建或打开数据库，如果不存在就创建，如果存在就打开
                SQLiteDatabase database = openOrCreateDatabase(mDatabase, MODE_PRIVATE, null);
                String str1=String.format("数据库%s创建%s",mDatabase,database==null?"失败":"成功");
                tv.setText(str1);
                break;
            case R.id.btn_delete:
                //删除数据库
                boolean result = deleteDatabase(mDatabase);
                String str2=String.format("数据库%s删除%s",mDatabase,result?"成功":"失败");
                tv.setText(str2);
                break;
        }
    }
}
